/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/vab/layouts'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    meta: {
      hidden: true,
    },
  },
]

export const asyncRoutes = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    redirect: '/index',
    meta: {
      title: '首页',
      icon: 'home-2-line',
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index'),
        meta: {
          title: window.globalTitle || '智慧平安小区管理平台',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/business',
    name: '/business',
    component: Layout,
    redirect: '/index',
    meta: {
      title: '业务管理',
      icon: 'community-line',
    },
    children: [
      {
        path: 'index',
        name: '/business/index',
        component: () => import('@/views/business/residentManagement/index'),
        meta: {
          title: '居民信息管理',
          icon: 'user-3-line',
        },
      },
      // {
      //   path: 'resident',
      //   name: 'Resident',
      //   component: () => import('@/views/business/residentManagement/index'),
      //   meta: {
      //     title: '居民信息',
      //     icon: 'user-3-line',
      //
      //   },
      // },
      {
        path: 'car',
        name: '/business/car',
        component: () => import('@/views/business/carInfo/index'),
        meta: {
          title: '车辆信息管理',
          icon: 'car-line',
        },
      },
      {
        path: 'equipment',
        name: '/business/equipment',
        component: () => import('@/views/business/equipmentManagement/index'),
        meta: {
          title: '设备信息管理',
          icon: 'camera-3-line',
        },
      },
      {
        path: 'faceImage',
        name: '/business/faceImage',
        component: () => import('@/views/business/faceRecord/index'),
        meta: {
          title: '人脸识别图像',
          icon: 'live-line',
        },
      },
      {
        path: 'vehicleImage',
        name: '/business/vehicleImage',
        component: () => import('@/views/business/carRecord/index'),
        meta: {
          title: '车辆识别图像',
          icon: 'live-line',
        },
      },
      {
        path: 'guardImage',
        name: '/business/guardImage',
        component: () => import('@/views/business/guardManagement/index'),
        meta: {
          title: '智慧门禁图像',
          icon: 'vidicon-line',
        },
      },
      {
        path: 'parkFeeRecord',
        name: '/business/parkFeeRecord',
        component: () => import('@/views/business/parkRecord/index'),
        meta: {
          title: '停车计费记录',
          icon: 'parking-box-line',
        },
      },
      {
        path: 'parkFee',
        name: '/business/parkFee',
        component: () => import('@/views/business/parkFee/index'),
        meta: {
          title: '停车计费标准',
          icon: 'parking-line',
        },
      },
      {
        path: 'statisticalInfo',
        name: '/business/statisticalInfo',
        component: () => import('@/views/business/statistic/index'),
        meta: {
          title: '综合统计信息',
          icon: 'bar-chart-2-line',
        },
      },
      {
        path: 'trafficInfo',
        name: '/business/trafficInfo',
        component: () => import('@/views/business/traffic/index'),
        meta: {
          title: '流量统计',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'vehicleImageRecord',
        name: '/business/vehicleImageRecord',
        component: () => import('@/views/business/carLog/index'),
        meta: {
          title: '车辆识别出入记录',
          icon: 'camera-3-line',
        },
      },
      {
        path: 'guardImageRecord',
        name: '/business/guardImageRecord',
        component: () => import('@/views/business/guardLog/index'),
        meta: {
          title: '智慧门禁出入记录',
          icon: 'camera-3-line',
        },
      },
    ],
  },
  {
    path: '/north',
    name: '/north',
    component: Layout,
    redirect: '/index',
    meta: {
      title: '北控推送',
      icon: 'send-to-back',
    },
    children: [
      {
        path: 'users',
        name: '/north/user',
        component: () => import('@/views/business/north-user/index'),
        meta: {
          title: '居民列表',
          icon: 'user-3-line',
        },
      },
      {
        path: 'guests',
        name: '/north/guest',
        component: () => import('@/views/business/north-guest/index'),
        meta: {
          title: '访客列表',
          icon: 'user-line',
        },
      },
      {
        path: 'houses',
        name: '/north/house',
        component: () => import('@/views/business/north-house/index'),
        meta: {
          title: '房屋列表',
          icon: 'hotel-line',
        },
      },
    ],
  },
  {
    path: '/setting',
    name: '/system',
    component: Layout,
    meta: {
      title: '系统管理',
      icon: 'settings-3-line',
      roles: ['SUPER'],
    },
    children: [
      {
        path: 'userManagement',
        name: '/system/user',
        component: () => import('@/views/setting/userManagement/index'),
        meta: {
          title: '用户管理',
          icon: 'home-2-line',
        },
      },
      {
        path: 'roleManagement',
        name: '/system/role',
        component: () => import('@/views/setting/roleManagement/index'),
        meta: {
          title: '角色管理',
          icon: 'admin-line',
        },
      },
      {
        path: 'menuManagement',
        name: '/system/menu',
        component: () => import('@/views/setting/menuManagement/index'),
        meta: {
          title: '菜单管理',
          icon: 'menu-2-line',
        },
      },
      {
        path: 'departmentManagement',
        name: '/system/dept',
        component: () => import('@/views/setting/deptManagement/index'),
        meta: {
          title: '部门管理',
          icon: 'group-line',
        },
      },
      {
        path: 'dictManagement',
        name: '/system/dictionary',
        component: () => import('@/views/setting/dictManagement/index'),
        meta: {
          title: '字典管理',
          icon: 'book-2-line',
        },
      },
      {
        path: 'exceptionLog',
        name: '/system/errorLog',
        component: () => import('@/views/setting/logManagement/exceptionLog'),
        meta: {
          title: '异常日志',
          icon: 'alarm-warning-line',
        },
      },
    ],
  },
  {
    path: '*',
    redirect: '/404',
    meta: {
      hidden: true,
    },
  },
]

const router = createRouter()

function fatteningRoutes(routes) {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

export function resetRouter(routes = constantRoutes) {
  routes.map((route) => {
    if (route.children) {
      route.children = fatteningRoutes(route.children)
    }
  })
  router.matcher = createRouter(routes).matcher
}

function createRouter(routes = constantRoutes) {
  return new VueRouter({
    base: publicPath,
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: routes,
  })
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

export default router
